package cn.afterturn.easypoi.test.excel.read;

import java.io.File;
import java.io.FileInputStream;
import java.util.Date;
import java.util.List;

import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.test.entity.MsgClient;
import cn.afterturn.easypoi.util.PoiPublicUtil;

/**
 *** 1、eventmodel方式，基于事件驱动,SAX的方式解析excel（.xlsx是基于OOXML的），CPU和内存消耗非常低，但是只能读不能写
 * 
 *** 2、usermodel，就是我们一般使用的方式，这种方式可以读可以写，但是CPU和内存消耗非常大
 * 
 *** 3、SXSSF，POI3.8以后开始支持，这种方式只能写excel
 */
public class BigDataRead {

	private static final Logger LOGGER = LoggerFactory.getLogger(BigDataRead.class);

	@Test
	public void test() {
		try {
			Date start = new Date();
			LOGGER.debug("start");
			ImportParams params = new ImportParams();
			params.setTitleRows(1);
			File file = new File(PoiPublicUtil.getWebRootPath("import/BigDataExport.xlsx"));
			FileInputStream inputstream = new FileInputStream(file);
			
			// sax 解决读取20万数据量时候 电脑内存占满 内存溢出
			List<MsgClient> result = ExcelImportUtil.importExcelBySax(inputstream, MsgClient.class, params);

			LOGGER.debug("end,time is {}", ((new Date().getTime() - start.getTime()) / 1000));
			Assert.assertTrue(result.size() == 200000);
		} catch (Exception e) {
		}
	}

	@Test
	public void test2000() {
		try {
			Date start = new Date();
			LOGGER.debug("start");
			ImportParams params = new ImportParams();
			params.setTitleRows(1);
			List<MsgClient> result = ExcelImportUtil.importExcel(new File(PoiPublicUtil.getWebRootPath("import/BigDataExport20000.xlsx")), MsgClient.class, params);
			LOGGER.debug("end,time is {}", ((new Date().getTime() - start.getTime()) / 1000));
			Assert.assertTrue(result.size() == 20000);
		} catch (Exception e) {
		}
	}

	@Test
	public void test5000() {
		try {
			Date start = new Date();
			LOGGER.debug("start");
			ImportParams params = new ImportParams();
			params.setTitleRows(1);
			List<MsgClient> result = ExcelImportUtil.importExcel(new File(PoiPublicUtil.getWebRootPath("import/BigDataExport50000.xlsx")), MsgClient.class, params);
			LOGGER.debug("end,time is {}", ((new Date().getTime() - start.getTime()) / 1000));
			Assert.assertTrue(result.size() == 50000);
		} catch (Exception e) {
		}
	}
}
